package com.jjb.www.ui.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.shape.layout.ShapeLinearLayout;
import com.hjq.shape.view.ShapeTextView;
import com.hjq.widget.layout.WrapRecyclerView;
import com.jjb.www.R;
import com.jjb.www.app.AppFragment;
import com.jjb.www.http.api.AetNumberFillInfoApi;
import com.jjb.www.http.api.AetNumberFillInfoV2Api;
import com.jjb.www.http.api.AnswerApi;
import com.jjb.www.http.api.AnswerV2Api;
import com.jjb.www.http.model.HttpData;
import com.jjb.www.ui.activity.SequentialPracticeActivity;
import com.jjb.www.ui.adapter.MultipleChoiceAdapter;
import com.jjb.www.ui.adapter.MultipleChoiceV2Adapter;
import com.jjb.www.ui.adapter.SingleChoiceAdapter;
import com.jjb.www.ui.adapter.SingleChoiceV2Adapter;
import com.jjb.www.utils.StringBufferUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *    author : Android CRG
 *    time   : 2018/10/18comprehensive
 *    desc   : 多选题Fragment
 */
public final class MultipleChoiceFragment extends AppFragment<SequentialPracticeActivity> implements BaseAdapter.OnItemClickListener {
    private static final String INTENT_KEY_ORDER_NUMBER = "order_number";
    private static final String INTENT_KEY_PAPERID = "paperid";
    private static final String INTENT_KEY_NUMBER_SCORE = "number_score";
    private static final String INTENT_KEY_NUMBER = "number";
    private static final String INTENT_KEY_EXAM_TYPE="exam_type";
    private static final String INTENT_KEY_FROM = "fromType";
    private ShapeTextView mTvNumber;
    private ShapeLinearLayout mRvQuestion;
    private ShapeTextView mTvTitle;
    private WrapRecyclerView mRvSelect;
    private MultipleChoiceAdapter adapter;
    private MultipleChoiceV2Adapter adapter2;
    private OnSelectListener mListener;
    //当前题的答案排序
    private String sort;
    //当前题的 试卷试题id
    private Integer fillId;
    private List<String> sortList;
    //当前答题的序号
    private String orderNumber;

    public static MultipleChoiceFragment newInstance(String fromType,Integer order_number,Integer paperid,Integer number_score,Integer number,Integer exam_type, OnSelectListener listener) {
        MultipleChoiceFragment fragment = new MultipleChoiceFragment();
        fragment.setOnSelectListener(listener);
        Bundle bundle = new Bundle();
        bundle.putInt(INTENT_KEY_ORDER_NUMBER, order_number);
        bundle.putInt(INTENT_KEY_PAPERID, paperid);
        bundle.putInt(INTENT_KEY_NUMBER_SCORE, number_score);
        bundle.putInt(INTENT_KEY_NUMBER, number);
        bundle.putInt(INTENT_KEY_EXAM_TYPE, exam_type);
        bundle.putString(INTENT_KEY_FROM, fromType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_multiple_choice;
    }

    @Override
    protected void initView() {
        mTvNumber = findViewById(R.id.tv_number);
        mRvQuestion = findViewById(R.id.rv_question);
        mTvTitle = findViewById(R.id.tv_title);
        mRvSelect = findViewById(R.id.rv_select);

        if (getString("fromType").equals("ceping")){
            adapter2 = new MultipleChoiceV2Adapter(getContext());
            adapter2.setOnItemClickListener(this);
            mRvSelect.setAdapter(adapter2);
        }else {
            adapter = new MultipleChoiceAdapter(getContext());
            adapter.setOnItemClickListener(this);
            mRvSelect.setAdapter(adapter);
        }

        mRvSelect.setLayoutManager(new LinearLayoutManager(getContext()));
    }
    public void setOnSelectListener(OnSelectListener listener){
        this.mListener= listener;
    }
    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (getString("fromType").equals("ceping")){
            getV2Paper();
        }else {
            getPaper();
        }

    }
    private void getV2Paper() {
        EasyHttp.get(this)
                .api(new AetNumberFillInfoV2Api()
                        .setOrder_number(getInt(INTENT_KEY_ORDER_NUMBER) + "")
                        .setPagerId(getInt(INTENT_KEY_PAPERID) + "")
                )
                .request(new HttpCallback<HttpData<AetNumberFillInfoV2Api.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<AetNumberFillInfoV2Api.Bean> data) {
                        mListener.questionDetailsV2(data.getData());
                        mRvQuestion.setVisibility(View.VISIBLE);

                        orderNumber = data.getData().getOrder_number()+"";
                        fillId = data.getData().getFill_id();
                        adapter2.setData(data.getData().getQuestion_answer());
                        //是否答题 0未答题 1已答题
                        if (data.getData().getIs_answer()==1){
                            for (int i = 0; i < data.getData().getQuestion_answer().size(); i++) {
                                if (data.getData().getQuestion_answer().get(i).getSort()==Integer.parseInt(data.getData().getAnswer_sort())){
                                    adapter2.setItemSel(i);
                                }
                            }
                        }
                        mTvNumber.setText("("+getInt(INTENT_KEY_NUMBER)+"/"+data.getData().getTotal_num()+"，每题"+getInt(INTENT_KEY_NUMBER_SCORE)+"分)");
                        mTvTitle.setText(data.getData().getOrder_number()+"、"+data.getData().getTitle());
                    }
                });
    }
    /**
     * 获取某道题的详情
     */
    private void getPaper() {
        EasyHttp.get(this)
                .api(new AetNumberFillInfoApi()
                        .setOrder_number(getInt(INTENT_KEY_ORDER_NUMBER) + "")
                        .setPagerId(getInt(INTENT_KEY_PAPERID) + "")
                )
                .request(new HttpCallback<HttpData<AetNumberFillInfoApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<AetNumberFillInfoApi.Bean> data) {
                        mListener.questionDetails(data.getData());
                        mRvQuestion.setVisibility(View.VISIBLE);
                        //是否答题 0未答题 1已答题
                        if (data.getData().getIs_answer()==1){
                            String[] temp = null;
                            temp = data.getData().getAnswer_sort().split(",");
                            for (AetNumberFillInfoApi.Bean.QuestionAnswerDTO bean:data.getData().getQuestion_answer()) {
                                for (String str:temp) {
                                    if (bean.getSort()==Integer.parseInt(str)){
                                        bean.setSelect(true);
                                    }
                                }
                            }
                        }
                        adapter.setData(data.getData().getQuestion_answer());
                        orderNumber = data.getData().getOrder_number()+"";
                        fillId = data.getData().getFill_id();
                        mTvNumber.setText("("+getInt(INTENT_KEY_NUMBER)+"/"+data.getData().getTotal_num()+"，每题"+getInt(INTENT_KEY_NUMBER_SCORE)+"分)");
                        mTvTitle.setText(data.getData().getOrder_number()+"、"+data.getData().getTitle());
                    }
                });
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        if (getString("fromType").equals("ceping")){
            AetNumberFillInfoV2Api.Bean.QuestionAnswerDTO bean = adapter2.getItem(position);
            if (bean.isSelect()){
                bean.setSelect(false);
            }else {
                bean.setSelect(true);
            }
            adapter2.setItem(position,bean);
            sortList = new ArrayList<>();
            for (AetNumberFillInfoV2Api.Bean.QuestionAnswerDTO bean2:adapter2.getData()) {
                if (bean.isSelect()){
                    sortList.add(bean.getSort()+"");

                }
            }
            sort = StringBufferUtils.arrToStr(sortList, ",");
            answerV2(fillId);
        }else {
            AetNumberFillInfoApi.Bean.QuestionAnswerDTO bean = adapter.getItem(position);
            if (bean.isSelect()){
                bean.setSelect(false);
            }else {
                bean.setSelect(true);
            }
            adapter.setItem(position,bean);
            sortList = new ArrayList<>();
            for (AetNumberFillInfoApi.Bean.QuestionAnswerDTO bean2:adapter.getData()) {
                if (bean.isSelect()){
                    sortList.add(bean.getSort()+"");

                }
            }
            sort = StringBufferUtils.arrToStr(sortList, ",");
            answer(fillId);
        }
    }
    /**
     * 答题
     * @param fill_id 试卷试题id
     */
    private void answer(Integer fill_id){
        EasyHttp.post(this)
                .api(new AnswerApi()
                        .setOrder_number(orderNumber)
                        .setPaper_id(getInt(INTENT_KEY_PAPERID) + "")
                        .setExam_type(getInt(INTENT_KEY_EXAM_TYPE)+"")
                        .setSort(sort)
                        .setFill_id(fill_id+"")
                )
                .request(new HttpCallback<HttpData<AnswerApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<AnswerApi.Bean> data) {

                    }
                });
    }
    /**
     * 答题
     * @param fill_id 试卷试题id
     */
    private void answerV2(Integer fill_id){
        EasyHttp.post(this)
                .api(new AnswerV2Api()
                        .setOrder_number(orderNumber)
                        .setPaper_id(getInt(INTENT_KEY_PAPERID) + "")
                        .setExam_type(getInt(INTENT_KEY_EXAM_TYPE)+"")
                        .setSort(sort)
                        .setFill_id(fill_id+"")
                )
                .request(new HttpCallback<HttpData<AnswerV2Api.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<AnswerV2Api.Bean> data) {

                    }
                });
    }
    public interface OnSelectListener{
        /**
         * 题详情
         * @param bean
         */
        void questionDetails(AetNumberFillInfoApi.Bean bean);

        //题详情
        void questionDetailsV2(AetNumberFillInfoV2Api.Bean bean);
    }
}